package com.amsp.oa.oademo.service.impl;

import com.amsp.oa.oademo.entity.DocumentDistribution;
import com.amsp.oa.oademo.entity.DocumentDistributionDetail;
import com.amsp.oa.oademo.mapper.DocumentDistributionMapper;
import com.amsp.oa.oademo.mapper.DocumentDistributionDetailMapper;
import com.amsp.oa.oademo.service.DocumentDistributionService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class DocumentDistributionServiceImpl extends ServiceImpl<DocumentDistributionMapper, DocumentDistribution> implements DocumentDistributionService {

    @Autowired
    private DocumentDistributionDetailMapper detailMapper;

    @Override
    public IPage<DocumentDistribution> getPage(Integer current, Integer size, String fileName, String fileNumber) {
        Page<DocumentDistribution> page = new Page<>(current, size);
        LambdaQueryWrapper<DocumentDistribution> wrapper = new LambdaQueryWrapper<>();
        
        if (StringUtils.hasText(fileName)) {
            wrapper.like(DocumentDistribution::getFileName, fileName);
        }
        if (StringUtils.hasText(fileNumber)) {
            wrapper.like(DocumentDistribution::getFileNumber, fileNumber);
        }
        
        wrapper.orderByDesc(DocumentDistribution::getCreateTime);
        return this.page(page, wrapper);
    }

    @Override
    public DocumentDistribution getDetailById(Long id) {
        DocumentDistribution distribution = this.getById(id);
        if (distribution != null) {
            LambdaQueryWrapper<DocumentDistributionDetail> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(DocumentDistributionDetail::getDistributionId, id);
            List<DocumentDistributionDetail> details = detailMapper.selectList(wrapper);
            distribution.setDistributionList(details);
        }
        return distribution;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveOrUpdateDistribution(DocumentDistribution distribution) {
        boolean result = this.saveOrUpdate(distribution);
        if (result && distribution.getDistributionList() != null) {
            // 删除旧的明细记录
            LambdaQueryWrapper<DocumentDistributionDetail> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(DocumentDistributionDetail::getDistributionId, distribution.getId());
            detailMapper.delete(wrapper);
            
            // 保存新的明细记录
            for (DocumentDistributionDetail detail : distribution.getDistributionList()) {
                detail.setDistributionId(distribution.getId());
                detailMapper.insert(detail);
            }
        }
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteById(Long id) {
        // 删除明细
        LambdaQueryWrapper<DocumentDistributionDetail> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DocumentDistributionDetail::getDistributionId, id);
        detailMapper.delete(wrapper);
        
        // 删除主表
        return this.removeById(id);
    }
} 